var ProjectObj={
	addProject:function(url){
		$('#projectAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if (!result.status){
                    
					$.messager.alert('错误提示!',result.message);
					
                } else if(result.id){
                	// add success
					//add -> edit
					$("input.projectFormId", "#projectAddForm").val(result.id);
					// desc tab
					var tab = $('#addProjectBox').tabs('getTab',1);  // get selected panel
					$('#addProjectBox').tabs('update', {
						tab: tab,
						options: {
							title: '项目详细信息',
							href: addProjectDescUrl+'?id='+result.id  // the new content URL
						}
					});//.tabs('select', '项目详细信息');
					
					$.messager.alert('提示!','添加成功！请继续填写项目描述！');
					
                } else {
                	$.messager.alert('修改成功!',result.message);
                }
            }

		});
	},
	editProject:function(url){
		$('#projectEditForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if (!result.status){
					$.messager.alert('错误提示!',result.message);
                } else {
					$.messager.alert('修改成功!',result.message);
                }
            }

		});
	},
    editPublicProject:function(url){
        $('#editPublicProjectForm').form('submit',{
            url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
                var result=eval('('+result+')');
                if (!result.status){
					$.messager.alert('错误提示!',result.message);
                } else {
					$.messager.alert('修改成功!',result.message);
                }
            }
        });
    },
	edit:function(url){
		var row = $('#projectList').datagrid('getSelected');
            if (row){
                url = url +'?id='+row.id;
				showTab(url,'修改项目');
            }

	},
	selectAgent:function(url){
		$('#searchAgentList').datagrid({
			url:url,
			fitColumns:true,
			rownumbers:true,
			pageSize:15,
			pageNumber:1,
			pageList:[2,5,10,15,20,25,30,40,50],
			columns:[[
			{field:'id',title:'id',checkbox:true},
			{field:'name',title:'姓名'},
			{field:'nickname',title:'昵称'},
			{field:'mobile',title:'手机号码'},
			{field:'email',title:'邮箱'},
			{field:'identity_num',title:'身份证号'}
			]],
			onLoadError:function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					var tab=$('#searcheAgentDilog').tabs('getSelected');
					var index=$('#searcheAgentDilog').tabs('getTabIndex',tab);
					$('#searcheAgentDilog').tabs('close',index);
				});
			},
			pagination:true
		});
		$('#searcheAgentDilog').dialog({
			title:'选择代理投资人',
			modal:true,
			width:500,
			height:450,
			buttons:'#searchAgentButton'
		});
	},
	selectAgentUser:function(id){
			var authRuleRow =$('#searchAgentList').datagrid('getSelections');
				if (authRuleRow.length<1){		            	
					$.messager.alert('提示','请选择代理人!','info');
				}else if(authRuleRow.length > 1){
					$.messager.alert('提示','只能选择一个代理人!','info');
				}else{
					
					$('#'+id).form('load',{agent_id:authRuleRow[0].id,agent_nickname:authRuleRow[0].nickname});
										
					$('#searcheAgentDilog').dialog('close');
					
				}
		
	},
	selectContractUser_one:function(id){
		var authRuleRow =$('#searchAgentList').datagrid('getSelections');
			if (authRuleRow.length<1){		            	
				$.messager.alert('提示','请选择代理人!','info');
			}else if(authRuleRow.length > 1){
				$.messager.alert('提示','只能选择一个代理人!','info');
			}else{
				
				$('#'+id).form('load',{borrower_id:authRuleRow[0].id,agent_nickname:authRuleRow[0].nickname});
									
				$('#searcheAgentDilog').dialog('close');
				
			}
	
},
    selectCompanyList: function (url) {
        $('#searchCompanyList').datagrid({
            url:url,
            fitColumns:true,
            rownumbers:true,
            singleSelect: false,
            pageSize:15,
            pageNumber:1,
            pageList:[2,5,10,15,20,25,30,40,50],
            columns:[[
                {field:'id',title:'id',checkbox:true},
                {field:'name',title:'商户简称'},
                {field:'all_name',title:'全称'},
                {field:'corporate',title:'法人'},
                {field:'licences_code',title:'营业执照'},
                {field:'identity_num',title:'身份证号'}
            ]],
            onLoadError:function(data){
                var info=eval('('+data.responseText+')');
                $.messager.confirm('错误提示',info.message,function(r){
                    var tab=$('#searchcompanyDilog').tabs('getSelected');
                    var index=$('#searchcompanyDilog').tabs('getTabIndex',tab);
                    $('#searchcompanyDilog').tabs('close',index);
                });
            },
            pagination:true
        });
        $('#searchcompanyDilog').dialog({
            title:'选择商户',
            modal:true,
            width:500,
            height:450,
            buttons:'#searchCompanyButton'
        });
    },
    selectCompany: function (id) {
        var row =$('#searchCompanyList').datagrid('getSelections');
        if (row.length<1){
            $.messager.alert('提示','请选择!','info');
        }else if(row.length > 1){
            $.messager.alert('提示','只能选择一个!','info');
        }else{

            $('#'+id).form('load',{company_id:row[0].id,company_name:row[0].name});

            $('#searchcompanyDilog').dialog('close');

        }
    },
	selectBorrowerList:function(){
		getSearchBorrowerListUrl = searchBorrowerListUrl + '?type='+$('#borrower_type').combobox('getValue');
		$('#searchBorrowerList').datagrid({
			url:getSearchBorrowerListUrl,
			fitColumns:true,
			rownumbers:true,
			pageSize:15,
			pageNumber:1,
			pageList:[2,5,10,15,20,25,30,40,50],
			columns:[[
			{field:'id',title:'id',checkbox:true},
			{field:'name',title:'姓名'},
			{field:'yn_company',title:'公司',formatter:checkYesNo},
			{field:'company_name',title:'公司名称'},
			{field:'mobile',title:'手机号码'},
			{field:'email',title:'邮箱'},
			{field:'identity_num',title:'身份证号'}
			
			]],
			onLoadError:function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					var tab=$('#searcheBorrowerDilog').tabs('getSelected');
					var index=$('#searcheBorrowerDilog').tabs('getTabIndex',tab);
					$('#searcheBorrowerDilog').tabs('close',index);
				});
			},
			pagination:true
		});
		$('#searcheBorrowerDilog').dialog({
			title:'选择借款人',
			modal:true,
			width:600,
			height:450,
			buttons:'#searBorrowerButton'
		});
	},
	selectContractList:function(getSearchBorrowerListUrl){
		//getSearchBorrowerListUrl = searchBorrowerListUrl + '?type=0';
		$('#searchBorrowerList').datagrid({
			url:getSearchBorrowerListUrl,
			fitColumns:true,
			rownumbers:true,
			pageSize:15,
			pageNumber:1,
        	singleSelect:true,
			pageList:[2,5,10,15,20,25,30,40,50],
			columns:[[
			{field:'id',title:'id',checkbox:true},
			{field:'name',title:'姓名'},
			{field:'yn_company',title:'公司',formatter:checkYesNo},
			{field:'company_name',title:'公司名称'},
			{field:'mobile',title:'手机号码'},
			{field:'email',title:'邮箱'},
			{field:'identity_num',title:'身份证号'},
			{field:'s_id',title:'公章',formatter:checkGongzhuang}
			]],
			onLoadError:function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					var tab=$('#searcheBorrowerDilog').tabs('getSelected');
					var index=$('#searcheBorrowerDilog').tabs('getTabIndex',tab);
					$('#searcheBorrowerDilog').tabs('close',index);
				});
			},
			pagination:true
		});
		$('#searcheBorrowerDilog').dialog({
			title:'选择借款人',
			modal:true,
			width:600,
			height:450,
			buttons:'#searBorrowerButton'
		});
	},
	selectBorrower:function(id){
			var authRuleRow =$('#searchBorrowerList').datagrid('getSelections');
				if (authRuleRow.length<1){		            	
					$.messager.alert('提示','请选择借款人!','info');
				}else if(authRuleRow.length > 1){
					$.messager.alert('提示','只能选择一个借款人!','info');
				}else{
					
					$('#'+id).form('load',{
														borrower_id:authRuleRow[0].id,
														borrower_name:authRuleRow[0].name,
														borrower_mobile:authRuleRow[0].mobile,
														company_name:authRuleRow[0].company_name,
														company_id:authRuleRow[0].company_id,
														identity_num:authRuleRow[0].identity_num
													});
										
					$('#searcheBorrowerDilog').dialog('close');

				}
		
	},
	selectContractBorrower:function(id){
		var authRuleRow =$('#searchBorrowerList').datagrid('getSelections');
		if (authRuleRow.length<1){		            	
			$.messager.alert('提示','请选择借款人!','info');
		}else if(authRuleRow.length > 1){
			$.messager.alert('提示','只能选择一个借款人!','info');
		}else{
			if(authRuleRow[0].s_id){
				$('#'+id).form('load',{
					borrower_id:authRuleRow[0].id,
					borrower_name:authRuleRow[0].name
				});		
				$('#searcheBorrowerDilog').dialog('close');
			}else{
				$.messager.alert('警告','您选择的借款人还没有印章，找法务制章后再签约！');
			}
		}
	
},
	
	showProject:function(showUrl,formId){
		var authRuleRow =$('#'+formId).datagrid('getSelections');
		if (authRuleRow.length>1){		            	
			$.messager.alert('提示','一次只能修改一条记录!','info');
		}else if(authRuleRow.length==1){
			showTab(showUrl+'?id='+authRuleRow[0].id,'项目预览');
		}else{
			$.messager.alert('提示','每次只能预览一个项目！');
		}
	},
	setProjectShowTime:function(){
		var projectRow =$('#approvalList').datagrid('getSelections');
		if (projectRow.length<1){		            	
			$.messager.alert('提示','请选择要修改的项目!','info');
		}else if(projectRow.length > 1){
			$.messager.alert('提示','只能选择一个项目!','info');
		}else{
				$('#addProjectShowTimeForm').form('load',{
					id:projectRow[0].id,
					show_time:timestampToDateTime(projectRow[0].show_time),
					public_time:timestampToDateTime(projectRow[0].public_time),
					yn_auto_split:projectRow[0].yn_auto_split,
					auto_split_time:timestampToDateTime(projectRow[0].auto_split_time),
					child_show_time:timestampToDateTime(projectRow[0].child_show_time),
					child_public_time:timestampToDateTime(projectRow[0].child_public_time)
				});
			}
		$('#setProjectShowTimeBox').dialog({
			title:'修改项目时间',
			buttons:'#setProjectShowTimeButton',
			width:700,
			height:400
		});
	},
	setProjectShowTimeSubmit:function(url){
		
		$.messager.confirm('提示！','您确定修改吗？',function(r){
			
			if(r){
				$('#addProjectShowTimeForm').form('submit',{
					url:url,
					onSubmit: function(){
						return $(this).form('validate');
					},
					success: function(result){
						var result=eval('('+result+')');
						if (!result.status){
							
							$.messager.alert('错误提示!',result.message);
							
						} else {
							
							$.messager.alert('提示!',result.message);
							
							$('#setProjectShowTimeBox').dialog('close')
							
							flushTab();
							
						}
					}

				});
			}
			
		})
		
		
		
	},
	showAll:function(){
		$('#projectList').datagrid({ queryParams:''});
	},
	showApproval:function(){
		$('#approvalList').datagrid({ queryParams:''});
	},
	formatTime:function(dataObj){
		dataObj.show_time = timestampToDateTime(dataObj.show_time);
		dataObj.public_time =timestampToDateTime(dataObj.public_time);
		dataObj.auto_split_time = timestampToDateTime(dataObj.auto_split_time);
		dataObj.child_show_time = timestampToDateTime(dataObj.child_show_time);
		dataObj.child_public_time = timestampToDateTime(dataObj.child_public_time);
		dataObj.interest_start_time = timestampToDate(dataObj.interest_start_time)
		dataObj.interest_end_time = timestampToDate(dataObj.interest_end_time)
		return dataObj;
	}
}

function checkGongzhuang(value){
	if(value){
        return '<font color="green">√</font>';
    }else{
        return '<font color="red">×</font>';
    }
}